<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of model
 *
 * @author chandu
 */
include CONFIG_PATH . 'db.php';

class model {

    public $db;
    public $userid;
    public $updates;
    public $datetime;
    public $keyFields;
    public $moduleName;
    public $query;
    public $result;
    public $result2;
    public $result_data;

    public function __construct() {
        $this->db = new db();
        $this->db->connect();
    }

    public function execute($query) {
        $this->result = mysql_query($query);
        return $this->result;
    }

    public function fetch_array($query) {
        $this->result = mysql_fetch_array($query);
        return $this->result;
    }

    public function fetch_row($query) {
        $this->result = mysql_fetch_row($query);
        return $this->result;
    }

    public function num_rows($query) {
        $this->result = mysql_num_rows($query);
        return $this->result;
    }

    public function get_permissions($table, $module) {
        $this->userid = $_SESSION['userid'];
        $this->query = "SELECT Addd, Del, Edit FROM $table WHERE userid = '$this->userid' and module = '$module'";
        $this->result = $this->execute($this->query);
        $this->result_data = $this->fetch_array($this->result);
        $_SESSION['Addd'] = $this->result_data['Addd'];
        $_SESSION['Del'] = $this->result_data['Del'];
        $_SESSION['Edit'] = $this->result_data['Edit'];
        return TRUE;
    }

    public function getKeyFields() {
        return $this->keyFields;
    }

    public function add($table, $data) {
        $retVal = true;

        $fields = $values = array();

        $keyfields = $this->getKeyFields();

        if (strlen($keyfields) > 0) {
            $whereString = '';
            $keyCount = count($data);
            for ($cnt = 0; $cnt < $keyCount; $cnt++) {
                $field = $keyfields[$cnt];

                if (strlen($whereString) == 0) {
                    $whereString = "WHERE $field = '$data[$field]'";
                } else {
                    $whereString = $whereString . " AND $field = '$data[$field]'";
                }
            }

            // check record existance and warn user if already exist and abort
            $sql = "select 1 from $table " . $whereString;
            echo $sql;
            $resultset = mysql_query($sql);
            if ($resultset . count > 0) {
                echo "record already exists!";
                $retVal = false;
            }
        }

        if ($retVal) {
            foreach (array_keys($data) as $key) {
                $fields[] = "`$key`";
                $values[] = "'" . mysql_real_escape_string($data[$key]) . "'";
            }

            $fields = implode(",", $fields);
            $values = implode(",", $values);

            $insert = mysql_query("INSERT INTO $table ($fields) VALUES ($values)");
            if ($insert) {
                return true;

                // return array( "mysql_error" => false,
                // "mysql_insert_id" => mysql_insert_id(),
                //"mysql_affected_rows" => mysql_affected_rows(),
                //"mysql_info" => mysql_info()
                // );
            } else {
                return array("mysql_error" => mysql_error());
            }
        }
    }

    public function delete($table, $chk, $col) {
        for ($i = 0; $i < count($chk); $i++) {
            if ($chk[$i] != "") {
                $strSQL = "DELETE FROM $table ";
                $strSQL .="WHERE $col = '" . $chk[$i] . "' ";
                $objQuery = mysql_query($strSQL);
            }
        }
        return true;
    }

    public
            function update($table, $id, $col, $array) {
        if (count($array) > 0) {

            foreach ($array as $key => $value) {

                $value = mysql_real_escape_string($value); // this is dedicated to @Jon
                $value = "'$value'";
                $this->updates[] = "$key = $value";
            }
        }
        $implodeArray = implode(', ', $this->updates);
        $sql = ("UPDATE $table  SET $implodeArray WHERE $col=$id");
        $update = mysql_query($sql) or die(mysql_error());
        if ($update) {
            return true;
        } else {
            return FALSE;
        }
    }

}

?>